<!--
 * @Author: [kangjie]
 * @Date: 2022-04-06 09:47:57
 * @LastEditors: [kangjie]
 * @LastEditTime: 2022-04-06 10:23:46
 * @Description: 
-->
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="./js/vue-2.6.12.js"></script>
  <script src="./js/vue-router-3.4.3.js"></script>
</head>

<body>
  <div id="app">
    <!--4.使用路由 -->
    <div>
      <router-link to="/user/id/1/username/zhangsan">去A路由</router-link>
    </div>
    <div>
      <router-view></router-view>
    </div>
  </div>
  <script>
    // 声明组件
    let myA = {
      data(){
        return {
          id:null,
          username:'',
        }
      },
      // created(){
      //   // 接收路由器对象传递的参数
      //   // 路由对象接受
      //   console.log(this.$route,'路由对象');
      //   this.id=this.$route.params.id;
      //   this.username=this.$route.params.username;
      // },
      // watch:{
      //   $route(to,from){
      //     // to是新路由 from是旧路由
      //     console.log(to,from,'监听路由参数变化');
      //     this.id=this.$route.params.id;
      //     this.username=this.$route.params.username;
      //   },
      // },
      // 监听路由变化 路由守卫
      beforeRouteUpdate(to,from,next){
        console.log(to.params,'路由参数');
        this.id=to.params.id;
        this.username=to.params.username;
        // 继续向下执行
        // next();
        // 阻断代码向下执行
        next(false);
      },
      template: `
        <div>A组件-{{id}}-{{username}}</div>
      `
    };
    //声明一个路由对象数组
    let routes = [
      {
        //  /user/id/1/username/zhangsan
        // /user/id/2/username/lisi
        path: '/user/id/:id/username/:username',
        component: myA
      }
    ];
    // 创建一个路由器对象实例
    let router=new VueRouter({
      routes:routes
    });
    // 注册路由对象
    new Vue({
      router,
      // 局部注册组件
      components: {
        'my-a': myA
      },
      el: '#app',
      data: {

      },
      methods: {

      }
    })
  </script>
</body>

</html>